package copyengine.ui.display.s3
{
    import flash.geom.Rectangle;

    import starling.textures.Texture;

    public final class Scale3Textures
    {
        /**
         * 水平
         */
        public static const DIRECTION_HORIZONTAL:String = "horizontal";

        /**
         * 垂直
         */
        public static const DIRECTION_VERTICAL:String = "vertical";

        /**
         * @private
         */
        private static const HELPER_RECTANGLE:Rectangle = new Rectangle();


        private var mTexture:Texture;

        private var mFirstRegionSize:Number;
        private var mSecondRegionSize:Number;

        private var mDirection:String;

        private var mFirst:Texture;
        private var mSecond:Texture;
        private var mThird:Texture;

        public function get texture():Texture {return this.mTexture;}

        public function get first():Texture {return mFirst;}

        public function get second():Texture {return mSecond;}

        public function get third():Texture {return mThird;}

        public function get direction():String {return mDirection;}

        public function get firstRegionSize():Number {return mFirstRegionSize;}

        public function get secondRegionSize():Number {return mSecondRegionSize;}

        /**
         * Constructor.
         *
         * @param texture            A Starling Texture to slice up into three regions. It is recommended to turn of mip-maps for best rendering results.
         * @param firstRegionSize    The size, in pixels, of the first of the three regions. This value should be based on the original texture dimensions, with no adjustments for scale factor.
         * @param secondRegionSize    The size, in pixels, of the second of the three regions. This value should be based on the original texture dimensions, with no adjustments for scale factor.
         * @param direction            Indicates if the regions should be positioned horizontally or vertically.
         */
        public function Scale3Textures(texture:Texture, firstRegionSize:Number, secondRegionSize:Number, direction:String = DIRECTION_HORIZONTAL)
        {
            this.mTexture = texture;
            this.mFirstRegionSize = firstRegionSize;
            this.mSecondRegionSize = secondRegionSize;
            this.mDirection = direction;
            this.initialize();
        }

        private function initialize():void
        {
            var textureFrame:Rectangle = this.mTexture.frame;
            if (!textureFrame) {
                textureFrame = HELPER_RECTANGLE;
                textureFrame.setTo(0, 0, this.mTexture.width, this.mTexture.height);
            }
            var thirdRegionSize:Number;
            if (this.mDirection == DIRECTION_VERTICAL) {
                thirdRegionSize = textureFrame.height - this.mFirstRegionSize - this.mSecondRegionSize;
            }
            else {
                thirdRegionSize = textureFrame.width - this.mFirstRegionSize - this.mSecondRegionSize;
            }

            if (this.mDirection == DIRECTION_VERTICAL) {
                var regionTopHeight:Number = this.mFirstRegionSize + textureFrame.y;
                var regionBottomHeight:Number = thirdRegionSize - (textureFrame.height - this.mTexture.height) - textureFrame.y;

                var hasTopFrame:Boolean = regionTopHeight != this.mFirstRegionSize;
                var hasRightFrame:Boolean = (textureFrame.width - textureFrame.x) != this.mTexture.width;
                var hasBottomFrame:Boolean = regionBottomHeight != thirdRegionSize;
                var hasLeftFrame:Boolean = textureFrame.x != 0;

                var firstRegion:Rectangle = new Rectangle(0, 0, this.mTexture.width, regionTopHeight);
                var firstFrame:Rectangle = (hasLeftFrame || hasRightFrame || hasTopFrame) ? new Rectangle(textureFrame.x, textureFrame.y, textureFrame.width, this.mFirstRegionSize) : null;
                this.mFirst = Texture.fromTexture(mTexture, firstRegion, firstFrame);

                var secondRegion:Rectangle = new Rectangle(0, regionTopHeight, this.mTexture.width, this.mSecondRegionSize);
                var secondFrame:Rectangle = (hasLeftFrame || hasRightFrame) ? new Rectangle(textureFrame.x, 0, textureFrame.width, this.mSecondRegionSize) : null;
                this.mSecond = Texture.fromTexture(mTexture, secondRegion, secondFrame);

                var thirdRegion:Rectangle = new Rectangle(0, regionTopHeight + this.mSecondRegionSize, this.mTexture.width, regionBottomHeight);
                var thirdFrame:Rectangle = (hasLeftFrame || hasRightFrame || hasBottomFrame) ? new Rectangle(textureFrame.x, 0, textureFrame.width, thirdRegionSize) : null;
                this.mThird = Texture.fromTexture(mTexture, thirdRegion, thirdFrame);
            }
            else //horizontal
            {
                var regionLeftWidth:Number = this.mFirstRegionSize + textureFrame.x;
                var regionRightWidth:Number = thirdRegionSize - (textureFrame.width - this.mTexture.width) - textureFrame.x;

                hasTopFrame = textureFrame.y != 0;
                hasRightFrame = regionRightWidth != thirdRegionSize;
                hasBottomFrame = (textureFrame.height - textureFrame.y) != this.mTexture.height;
                hasLeftFrame = regionLeftWidth != this.mFirstRegionSize;

                firstRegion = new Rectangle(0, 0, regionLeftWidth, this.mTexture.height);
                firstFrame = (hasLeftFrame || hasTopFrame || hasBottomFrame) ? new Rectangle(textureFrame.x, textureFrame.y, this.mFirstRegionSize, textureFrame.height) : null;
                this.mFirst = Texture.fromTexture(mTexture, firstRegion, firstFrame);

                secondRegion = new Rectangle(regionLeftWidth, 0, this.mSecondRegionSize, this.mTexture.height);
                secondFrame = (hasTopFrame || hasBottomFrame) ? new Rectangle(0, textureFrame.y, this.mSecondRegionSize, textureFrame.height) : null;
                this.mSecond = Texture.fromTexture(mTexture, secondRegion, secondFrame);

                thirdRegion = new Rectangle(regionLeftWidth + this.mSecondRegionSize, 0, regionRightWidth, this.mTexture.height);
                thirdFrame = (hasTopFrame || hasBottomFrame || hasRightFrame) ? new Rectangle(0, textureFrame.y, thirdRegionSize, textureFrame.height) : null;
                this.mThird = Texture.fromTexture(mTexture, thirdRegion, thirdFrame);
            }
        }


    }
}
